﻿function upload() {
    $("#upload_btn").click(function () {
        $.ajax({
            url: '/op/upload',
            method: 'post'
        })
    })
}



function download() {
    $("#download_btn").click(function () {
        $.ajax({
            url: '/op/download',
            method: 'post'
        })
    })
}

function back() {
    $("#back_btn").click(function () {
        $.ajax({
            url: '/op/back',
            method: 'post'
        })
        window.location.replace('/index')
    })
   
    
    
}

function deleteFile(id) {
    if (id !== undefined) {
        var isDir = false
        var fileName = id
        if (id.indexOf('.') !== -1) {
            isDir = true
        }
        $.ajax({
            url: '/op/delete',
            data: {
                isDir: isDir,
                fileName: fileName
            },
            method: 'post',
            success: function (res) {
                if (res == '400') {
                    alert('权限不足或者文件夹不为空')

                } else {
                    alert('删除成功')
                }
            },
            complete: function (xhr, status) {
                location.reload()
            }

        })
    }
}


function localSync(id) {
    
    if (id !== undefined && id.indexOf('.') !== -1) {
        var fileName = id
        $.ajax({
            url: '/op/localSync',
            data: {
                fileName: fileName
            },
            method: 'post',
            complete: function (xhr, status) {
                location.reload()
            }
        })
        
    }
}

function changeState(state, filename) {
    var currentState = state
    var filename = filename
    $.ajax({
        url: '/op/changeIgnore',
        data: {
            currentState: currentState,
            filename: filename
        },
        method: 'post',
        complete: function (xhr, status) {
            location.reload()
        }
    })
}

function ftpSync(id) {

    if (id !== undefined && id.indexOf('.') !== -1) {
        var fileName = id
        $.ajax({
            url: '/op/ftpSync',
            data: {
                fileName: fileName
            },
            method: 'post',
            complete: function (xhr, status) {
                location.reload()
            }
        })

    }
}


function getList(id) {
    
    if (id !== undefined && id.indexOf('.') == -1) {
        var currentPath =  '/' + id
        $.ajax({
            url: '/',
            data: {
                path: currentPath
            },
            method: 'post'
        })
        window.location.replace('/index')
    }
   
}

function history() {
    $("#show_btn").click(function () {
        $.ajax({
            url: '/history',
            method: 'get'
        })
    })
}






// 整个网页加载完再去执行函数
$(function () {
    upload()
    back()
    download()
    getList(id)
    localSync(id)
    ftpSync(id)
    deleteFile(id)
    changeState(state, filename)
    history()
});